#include "head.h"

struct linkqueue* create_linkqueue(void)
{
	struct linkqueue* q = (struct linkqueue*)malloc(sizeof(struct linkqueue));
	q->front = q->rear = (struct linklist*)malloc(sizeof(struct linklist));
	q->front->next = NULL;
	q->size = 0;
	return q;
}

int is_empty_linkqueue(struct linkqueue * q)
{
	return q->rear == q->front;
}

int input_linkqueue(struct linkmsg* data, struct linkqueue* q)
{
	if(q->size >= SIZE_LEN)
	{
		return -1;
	}
	//printf("data = %d\n", data->text[0]);
	struct linklist *node = (struct linklist*)malloc(sizeof(struct linklist));	
	node->msg = *data;
	node->next = NULL;	
	q->rear->next = node;
	q->rear = node;
	q->size ++;
	//printf("%d\n", (q->rear->msg).text[0]);
	return 0;
}

int output_linkqueue(struct linkqueue* q, struct linkmsg* lmsg)
{
	if(is_empty_linkqueue(q))
	{
		printf("q is empty\n");
		return -1;
	}
	struct linklist *del = q->front;
	memcpy(lmsg, &del->next->msg, sizeof(struct linkmsg));
	q->front = q->front->next;
	free(del);
	q->size --; 
	del = NULL;
	return 0;
}
int get_size(struct linkqueue *q)
{
	return q->size;
}
int show_linkqueue (struct linkqueue* q, void (*func)(struct linkmsg *msg))
{
	if (is_empty_linkqueue(q))
	{
		printf("q is empty\n");
		return -1;
	}
	struct linklist *p = q->front;
	while(p->next != NULL)
	{
		func(&(p->next->msg));
		p = p->next;
	}
	return 0;
}
